--每日场景答题
--#include "data\config\activityconfig\DailyActivity\SceneAnswerConfig.lua" once

--[[
注册一些事件
]]
function ResisterSceneAnswerProc()
	--print("ResisterSceneAnswerProc...")
	SceneExitDispatcher.register( SceneAnswerConfig.sceneId, OnSceneAnswerExit )			--注册离开场景、副本后触发的事件
end

--[[
已经退出副本或场景
注意：手工退出、下线、被T出，GM指令Move出都会触发
]]
function OnSceneAnswerExit( sysarg, sceneId, fubenId )
	--print("OnSceneAnswerExit, sceneId="..sceneId..", fubenId="..fubenId)
	CloseActivityRightPanel(sysarg)
	Actor.closeDialogs( sysarg, enLinkAnswerWnd ) 			--关闭答题面板
	Actor.collectOperate( sysarg, 1 )						--刷外观
end

--[[
场景答题正在进行中
]]
function InSceneAnswerDoing()
	local dVar = System.getDyanmicVar()
	--print("InSceneAnswerDoing, sceneQuestionIdx="..dVar.sceneQuestionIdx)
	if dVar.sceneQuestionIdx and dVar.sceneQuestionIdx >= 0 then
		return true
	end
	return false
end

--[[
场景答题开始
]]
function SceneAnswerStart( )
	local dVar = System.getDyanmicVar()
	dVar.sceneQuestionIdx 		= 0			--活动开启时间
	dVar.sceneAnswerStartTime	= System.getCurrMiniTime()
	dVar.sceneQuestionScore 	= nil
	dVar.nextQuestionTime 		= nil
	dVar.checkQuestionTime 		= nil

	ClearCommonRank( SceneAnswerRank )		--清空活动答题积分榜

	if not DrawSceneAnswerQuestionFromBank( SceneAnswerConfig.answerNum ) then
		print("DrawSceneAnswerQuestion error !")
		return
	end

	local NpcPtr = System.getGlobalNpc()
	Actor.unregScriptCallback( NpcPtr, "StopSceneAnswer" )
	Actor.regScriptTimer( NpcPtr, 0, SceneAnswerConfig.activityTime*1000, SceneAnswerConfig.activityTime*1000, 1, "StopSceneAnswer" )


	Fuben.unregScriptCallback(NpcPtr, "LoopSceneAnswerSendQuestion")
	Actor.regScriptTimer(NpcPtr, 0, SceneAnswerConfig.beginTime*1000, 0, 1, "LoopSceneAnswerSendQuestion")

	System.broadcastTipmsg( Lang.ScriptTips.SceneAnswer001, ttScreenCenter + ttChatWindow )	

	CommonTickPlayerOutScene( SceneAnswerConfig.sceneId )
end

--[[活动结束
此方法，由定时器触发
]]
function StopSceneAnswer()
	local NpcPtr = System.getGlobalNpc()
	Actor.unregScriptCallback( NpcPtr, "StopSceneAnswer" )
	Fuben.unregScriptCallback( NpcPtr, "LoopSceneAnswerSendQuestion" )
	CommonTickPlayerOutScene( SceneAnswerConfig.sceneId )
	System.broadcastTipmsg( Lang.ScriptTips.SceneAnswer012, ttScreenCenter + ttChatWindow )
end

--[[
场景答题结束
]]
function SceneAnswerStop()
	local dVar = System.getDyanmicVar()
	dVar.sceneQuestionIdx  		= nil
	dVar.sceneAnswerStartTime 	= nil
	dVar.sceneQuestionScore 	= nil
	dVar.nextQuestionTime 		= nil
	dVar.checkQuestionTime 		= nil
	System.broadcastTipmsg( Lang.ScriptTips.SceneAnswer002, ttScreenCenter + ttChatWindow )
end


--[[
进入答题场景
]]
function EnterAnswerScene( sysarg )
	if not InSceneAnswerDoing() then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.SceneAnswer003, ttFlyTip)
		return
	end

	if CommonEnterScence( sysarg, SceneAnswerConfig, 0, "", nil, nil ) then
		InitAnswerScenePanel( sysarg )

		local dVar = System.getDyanmicVar()
		if dVar.sceneAnswerStartTime and dVar.sceneAnswerStartTime > 0 then
			local boardStr 	= Lang.ScriptTips.SceneAnswer004				--注意：字符串中要带有一个 %s
			local waitTime	= dVar.sceneAnswerStartTime + SceneAnswerConfig.beginTime - System.getCurrMiniTime()
			if waitTime > 0 then		--题目尚未开始
				OpenBoardPanel( sysarg, enBoardPanelStyle_CountDown1, boardStr, waitTime+1 )
			else
				local checkTime	= GetSceneAnswerCheckQuestionTime()			--等待当前题目判断答案
				if checkTime <= 0 then 										--等待新题目
					local nextTime 	= GetSceneAnswerNextQuestionTime()			--等待下一题
					if nextTime > 0 then
						local now 	= System.getCurrMiniTime()
						lastTime = nextTime - now
						if lastTime < 0 then
							lastTime = 0
						end
						OpenBoardPanel( sysarg, enBoardPanelStyle_CountDown1, Lang.ScriptTips.SceneAnswer006, lastTime+1 )
					else
						OpenBoardPanel( sysarg, enBoardPanelStyle_Default, Lang.ScriptTips.AnswerActivity006, 0 )
					end
				else 		--当前题目等待check
					local question = GetSceneAnswerCurQuestion()
					if not question then
						return
					end

					local now 	= System.getCurrMiniTime()
					lastTime = checkTime - now
					if lastTime < 0 then
						lastTime = 0
					end

					local closePanel = false
					SendCommonAnswerQuestion( sysarg, SCENE_ANSWER_ACTIVITY, nowQestionIdx, lastTime, question, score, closePanel )
					--SendCommonRankSelfData( sysarg, enCommonRankType_AnswerToday )
					SendCommonRankSelfData( sysarg, enCommonRankType_SceneAnswer )
					OpenBoardPanel( sysarg, enBoardPanelStyle_CountDown1, Lang.ScriptTips.SceneAnswer005, lastTime+1 )
				end
			end

			--检查当天是否第1次执行(进入场景，每天只计算第一次进入场景)
			if CheckActorDailyDoneTimes( sysarg, enDailyOpTimes_SceneAnswer, 0, 1 ) then
				AddActorDailyDoneTimes( sysarg, enDailyOpTimes_SceneAnswer, 0, 1 )
				Actor.triggerAchieveEvent(sysarg, 44, 9, 1)				--触发场景答题活动成就
				--print("EnterAnswerScene, first do")
			end
		end
	end
end

function InitAnswerScenePanel( sysarg )
	local dVar 	= System.getDyanmicVar()
	local now 	= System.getCurrMiniTime()
	local sceneAnswerStartTime = dVar.sceneAnswerStartTime or 0

	local contStyle 	= 6
	local buttons 		= {6,1}
	local activityId 	= enRightPanelActivity_SceneAnswer
	local subActivityId = SCENE_ANSWER_ACTIVITY 					--活动答题
	local title			= Lang.ScriptTips.SceneAnswerName
	local answerCfg 	= {}
	answerCfg.restTime 	= ( sceneAnswerStartTime +  SceneAnswerConfig.activityTime ) - now
	if answerCfg.restTime < 0 then
		answerCfg.restTime = 0
	end
	answerCfg.rankType	= enCommonRankType_SceneAnswer			--enCommonRankType_AnswerToday
	OpenCommonStylePanel( sysarg, 2, activityId, subActivityId, title, answerCfg, contStyle, buttons  )
end

function ChangeAnswerScenePanel( sysarg )
	local activitySts 	= 1
	local contStyle 	= 6
	local buttons1 		= {6,1}
	local buttons2 		= {6,1}
	local answerCfg 	= {}
	answerCfg.rankType	= enCommonRankType_SceneAnswer			--enCommonRankType_AnswerToday
	ChangeCommonStylePanel( sysarg, activitySts, contStyle, answerCfg, buttons1, buttons2 )
end

--[[
打开题目面板
]]
function OpenSceneAnswerQuestionPanel( sysarg )
	local nowQestionIdx = GetSceneAnswerCurQuestionIdx()			--当前题目
	local question = GetSceneAnswerCurQuestion()
	if not question then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.AnswerActivity006, ttFlyTip)
		return
	end

	local nextTime 	= GetSceneAnswerNextQuestionTime()			--等待下一题
	local checkTime	= GetSceneAnswerCheckQuestionTime()			--等待当前题目判断答案
	--print("OpenSceneAnswerQuestionPanel nextTime="..nextTime..", checkTime="..checkTime)
	if checkTime <= 0 then 										
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.SceneAnswer007, ttFlyTip)
		return
	end

	local now 	= System.getCurrMiniTime()
	lastTime = checkTime - now
	if lastTime < 0 then
		lastTime = 0
	end
		--print("OpenSceneAnswerQuestionPanel checkTime="..checkTime..", lastTime="..lastTime)
	local score 	= GetSceneAnswerCurQuestionScore()
	--local boardStr 	= Lang.ScriptTips.SceneAnswer005
	local closePanel = false			--中途打开，不关闭倒计时面板
	SendCommonAnswerQuestion( sysarg, SCENE_ANSWER_ACTIVITY, nowQestionIdx, lastTime, question, score, closePanel )
	--SendCommonRankSelfData( sysarg, enCommonRankType_AnswerToday )
	SendCommonRankSelfData( sysarg, enCommonRankType_SceneAnswer )
end

--[[
从题库中抽取题目
]]
function DrawSceneAnswerQuestionFromBank( answerNum )
	local dVar = System.getDyanmicVar()
	dVar.sceneAnswersIdx = {}
	local sceneAnswersIdx = CommonAnswerDrawAnswersIdxInBank( SceneAnswerConfig.answerBankId, answerNum )
	for i, answerIdx in ipairs( sceneAnswersIdx or {}) do
		dVar.sceneAnswersIdx[i] = answerIdx
		--print("DrawAnswerFromAnswerBankForActivity idx="..i..", answerIdx="..answerIdx)
	end

	for i=1, answerNum do
		if not dVar.sceneAnswersIdx[i] then
			return false
		end
	end
	return true
end

--[[
场景答题发送题目
]]
function LoopSceneAnswerSendQuestion()
	--print("LoopSceneAnswerSendQuestion...")
	local newQestionIdx = GetSceneAnswerCurQuestionIdx() + 1 			--下一题开始
	SetSceneAnswerCurQuestionIdx( newQestionIdx )
	--print("LoopSceneAnswerSendQuestion newQestionIdx="..newQestionIdx)

	local question = GetSceneAnswerCurQuestion()
	if not question then
		print("LoopSceneAnswerSendQuestion, no question")
		SceneAnswerStop()
		return
	end

	local now = System.getCurrMiniTime()
	SetSceneAnswerNextQuestionTime( nil )								--下一题时间置空
	SetSceneAnswerCheckQuestionTime( now+SceneAnswerConfig.answerCd )	--检查答案的时间
	
	local lastTime 		= SceneAnswerConfig.answerCd
	local activityId 	= SCENE_ANSWER_ACTIVITY
	local answerArea 	= nil
	local levelLimit 	= SceneAnswerConfig.enterLevelLimit
	local idx,score 	= GetItemIdxRandNoWeight( question.score ) 		--答题积分
	if score <= 0 then
		score = SceneAnswerConfig.answerAward.rightAward.addPoint
	end
	--print("LoopSceneAnswerSendQuestion newQestionIdx="..newQestionIdx..", score="..score)
	SetSceneAnswerCurQuestionScore( score )

	BroadCommonAnswerQuestionInArea( activityId, SceneAnswerConfig.sceneId, answerArea, lastTime, newQestionIdx, question, score, levelLimit )

	local NpcPtr = System.getGlobalNpc()
	Fuben.unregScriptCallback(NpcPtr, "LoopSceneAnswerCheck")
	Actor.regScriptTimer(NpcPtr, 0, SceneAnswerConfig.answerCd*1000, 0, 1, "LoopSceneAnswerCheck")
end


--[[
答题完毕，到发送下一题题目的时间
]]
function LoopSceneAnswerCheck( )
	local questionIdx = GetSceneAnswerCurQuestionIdx()
	if questionIdx <= 0 then
		print("LoopSceneAnswerCheck, scene answer over")
		return
	end
	--print("LoopSceneAnswerCheck, questionIdx="..questionIdx)
	CheckSceneAnswer( questionIdx )			--检查答案

	local NpcPtr = System.getGlobalNpc()
	if questionIdx < SceneAnswerConfig.answerNum then 		--答题还在进行中
		Fuben.unregScriptCallback(NpcPtr, "LoopSceneAnswerSendQuestion")
		Actor.regScriptTimer2S(NpcPtr, 0, SceneAnswerConfig.waitCd*1000, 0, 1, "LoopSceneAnswerSendQuestion")
		FreshCommonMonstersRand( SceneAnswerConfig.monsters, 0, true, SceneAnswerConfig.monstersNum )		--刷出一些答题怪
	else 			--最后一题完毕
		SceneAnswerStop()
	end
end

function GetSceneAnswerCurQuestionIdx()
	local dVar = System.getDyanmicVar()
	return dVar.sceneQuestionIdx or 0
end

function SetSceneAnswerCurQuestionIdx( newQuestionIdx )
	local dVar = System.getDyanmicVar()
	dVar.sceneQuestionIdx = newQuestionIdx
end

function GetSceneAnswerCurQuestionScore( )
	local dVar = System.getDyanmicVar()
	return dVar.sceneQuestionScore or 0
end


function SetSceneAnswerCurQuestionScore( newScore )
	local dVar = System.getDyanmicVar()
	dVar.sceneQuestionScore = newScore
end

--[[
当前题目的检查时间
]]
function GetSceneAnswerCheckQuestionTime()
	local dVar = System.getDyanmicVar()
	return dVar.checkQuestionTime or 0
end

--[[
设置题目的检查时间
]]
function SetSceneAnswerCheckQuestionTime( newTime )
	local dVar = System.getDyanmicVar()
	dVar.checkQuestionTime = newTime
end

--[[
下一题下发的时间
]]
function GetSceneAnswerNextQuestionTime()
	local dVar = System.getDyanmicVar()
	return dVar.nextQuestionTime or 0
end

--[[
设置下一题下发的时间
]]
function SetSceneAnswerNextQuestionTime( newTime )
	local dVar = System.getDyanmicVar()
	dVar.nextQuestionTime = newTime
end

--[[
获取当前题目
]]
function GetSceneAnswerCurQuestion()
	local dVar = System.getDyanmicVar()
	local questionIdx = GetSceneAnswerCurQuestionIdx()
	local idx = dVar.sceneAnswersIdx[questionIdx]
	if not idx then
		return nil
	end
	return  GetCommonAnswerQuestionByIdx( SceneAnswerConfig.answerBankId, idx )
end


--[[根据区域坐标范围，判断玩家是否答题正确
questionIdx：第N轮答题
]]
function CheckSceneAnswer( questionIdx )
	--print("CheckSceneAnswer, questionIdx="..questionIdx)
	local question = GetSceneAnswerCurQuestion()
	if not question then
		print("CheckSceneAnswer, no question")
		return
	end

	local nextTime = 0
	if questionIdx < SceneAnswerConfig.answerNum then 		--答题还在进行中
		local now = System.getCurrMiniTime()
		SetSceneAnswerNextQuestionTime( now + SceneAnswerConfig.waitCd )		--下一题时间
		SetSceneAnswerCheckQuestionTime( nil )									--检查答案时间置空
		nextTime = SceneAnswerConfig.waitCd
	else
		SetSceneAnswerNextQuestionTime( nil )									--下一题时间置空
		SetSceneAnswerCheckQuestionTime( nil )									--检查答案时间置空
	end
	local playerList = LuaHelp.getSceneActorListById( SceneAnswerConfig.sceneId )
	for k, player in ipairs( playerList or {} ) do
		CheckSceneAnswerActorAnswer( player, SCENE_ANSWER_ACTIVITY, questionIdx, question, nextTime )
	end

	--积分判定后再全部刷新面板
	for k, player in ipairs( playerList or {} ) do
		ChangeAnswerScenePanel( player )
	end
end

--获取答题区域
--return 1-A区域，2-B区域，3-C区域，4-D区域，0-都不是
--function GetPlayerAnswerIdx(sysarg)
function GetSceneAnswerIdxByArea(sysarg)
	local answerIdx = Actor.getMapAttriValue(sysarg, aaAnswerArea)
	if answerIdx >= 1 and answerIdx <= 4 then
		return answerIdx
	else
		return 0
	end
end

function CheckSceneAnswerActorAnswer( sysarg, activityId, questionIdx, question, nextTime )
	local myAnswerIdx 		= GetSceneAnswerIdxByArea( sysarg )					--根据玩家站立的区域，判断答案
	local rightAnswerIdx 	= GetCommonAnswerRightAnswerIdx( question )
	--print("CheckSceneAnswerActorAnswer, questionIdx="..questionIdx..", myAnswerIdx="..myAnswerIdx..", rightAnswerIdx="..rightAnswerIdx)

	local addScore = 0
	if rightAnswerIdx > 0 and myAnswerIdx == rightAnswerIdx then
		local score 	= GetSceneAnswerCurQuestionScore()
		local rate 		= GetSceneAnswerScoreRate( sysarg )
		addScore 	= score * rate
		UpdateAnswerRankPoint( sysarg, enCommonRankType_AnswerToday, addScore )			--加周榜积分积分
		UpdateAnswerRankPoint( sysarg, enCommonRankType_SceneAnswer, addScore )			--加答题活动榜积分
	end
	SendCommonAnswerCheckResult( sysarg, activityId, questionIdx, myAnswerIdx, rightAnswerIdx, addScore )
	--ChangeAnswerScenePanel( sysarg )

	if nextTime > 0 then
		local boardStr = Lang.ScriptTips.SceneAnswer006				--注意：字符串中要带有一个 %s
		OpenBoardPanel( sysarg, enBoardPanelStyle_CountDown1, boardStr, nextTime+1 )
	end
end


function DoSceneAnswerActorAnswer( sysarg, roundIdx, opType, param )
	if opType == ANSWER_OPTYPE_OPEN_ANSWERDLG then
		OpenSceneAnswerQuestionPanel( sysarg )
	end
end

--[[
--sysarg:归属玩家
--lastHitKiller：最后一击玩家
注意：怪物死亡时，玩家可能不在线，要确保副本进度能够继续进行
]]
function OnSceneAnswerMonsterDie( monster, sysarg, monId, lastHitKiller )
	local effectIdx,effect = GetItemIdxRandNoWeight( SceneAnswerConfig.monsterGatherEffext )
	--print( "OnSceneAnswerMonsterDie, monId="..monId..", effectIdx="..effectIdx )
	if effect.buffId then
		ClearSceneAnswerBuff( sysarg )
		Actor.addBuffById( sysarg, effect.buffId )
	elseif effect.removeBuff then
		ClearSceneAnswerBuff( sysarg )
	end

	if effect.msg then
		Actor.sendTipmsg( sysarg, effect.msg, ttFlyTip+ttChatWindow )
		--print( "OnSceneAnswerMonsterDie, msg="..effect.msg )
	end
end


function ClearSceneAnswerBuff( sysarg )
	for idx, ansBuf in ipairs( SceneAnswerConfig.answerBuff ) do
		if Actor.hasBuffById( sysarg, ansBuf.buffId ) then
			--print( "ClearSceneAnswerBuff, buffId="..ansBuf.buffId )
			Actor.delBuffById( sysarg, ansBuf.buffId )
		end
	end
end

function GetSceneAnswerScoreRate( sysarg )
	for idx, ansBuf in ipairs( SceneAnswerConfig.answerBuff ) do
		if Actor.hasBuffById( sysarg, ansBuf.buffId ) then
			return ansBuf.scoreRate
		end
	end
	return 1
end

ResisterSceneAnswerProc()

